<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of news
 *
 * @author HungPV-PC
 */
class news extends MX_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model('news/news_model');
    }

    function show($row = 0) {
        if($this->session->userdata('login')!=1)
        {
            redirect('admin/login');
        }
        //paging
        include('paging.php');
        $config['per_page'] = 10;
        $config['base_url'] = base_url() . 'admin/news/show/';

        $config['total_rows'] = $this->news_model->get_count();
        $config['cur_page'] = $row;
        $this->pagination->initialize($config);
        $data['list_link'] = $this->pagination->create_links();

        $news = $this->news_model->get_news($row, $config['per_page']);
        
        $data['news'] = $news;
        $data['view'] = 'news_show';
        $this->load->view('back_end/template', $data);
    }

    function add() {
        if($this->session->userdata('login')!=1)
        {
            redirect('admin/login');
        }
        $flag = TRUE;
        $hide = TRUE;

        if ($this->input->post('name')) {
            $hide = FALSE;

            $name = $this->input->post('name');
            $description = $this->input->post('description');
            $content = $this->input->post('content');
            $image = $this->input->post('photo_url');
            

            $id = $this->news_model->add($name, $description, $content,$image);

            redirect('admin/news/show');
        }

        $data['view'] = 'news_add';
        $this->load->view('back_end/template', $data);
    }

    function edit($id) {
        if($this->session->userdata('login')!=1)
        {
            redirect('admin/login');
        }
        if ($this->input->post('name')) {

            $id = $this->input->post('id');
            $name = $this->input->post('name');
            $description = $this->input->post('description');
            $content = $this->input->post('content');
            $image = $this->input->post('photo_url');
            $this->news_model->edit($id, $name, $description, $content,$image);
        }
        
        $news = $this->news_model->get($id);
        
        $data['news'] = $news;       
        $data['view'] = 'news_edit';
        $this->load->view('back_end/template', $data);
    }

    function delete($id) {
        $this->news_model->delete($id);
    }

}

?>
